//
//  XHPermissionsViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2019/3/28.
//  Copyright © 2019 xiaohan. All rights reserved.
//

#import "XHPermissionsViewController.h"
#import "EOSService.h"

@interface XHPermissionsViewController ()
@property (nonatomic,strong) UILabel*accountLab;
@property (nonatomic,strong) UILabel*ownerLab;
@property (nonatomic,strong) UILabel*ownerKeyLab;
@property (nonatomic,strong) UILabel*ownerWeightLab;
@property (nonatomic,strong) UILabel*activeLab;
@property (nonatomic,strong) UILabel*activeKeyLab;
@property (nonatomic,strong) UILabel*activeWeightLab;
@property (nonatomic,strong) EOSService *eosService;
@property (nonatomic,strong) NSArray <EOSKey*>*ownerKeys;
@property (nonatomic,strong) NSArray <EOSKey*>*activeKeys;

@end

@implementation XHPermissionsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self getAccountInfo];
}

- (void)getAccountInfo{
    self.accountLab.text = self.wallet.wallet_name;
    [self.eosService getAccountInfoComplete:^(BOOL success, NSString * _Nonnull errorMsg) {
        if (success) {
            if (self.eosService.permission.length) {
                [[XHDataBaseManager shareInstance]updateWalletToDB:self.eosService.wallet];
            }else{
                [XAlertTool alertTitle:@"提示" message:@"当前账号已经没有权限，是否删除钱包？" cancelAction:^{
                    
                } sureAction:^{
                    if ([[XHDataBaseManager shareInstance]removeWallet:self.wallet.wallet_uid]) {
                        if ([self.wallet.wallet_uid isEqualToString:CURRENT_WALLET_ID]) {
                            [WalletTool changeCurrentWallet];
                        }
                        [[WalletTool sharedWalletTool].walletUseFingerprint removeObjectForKey:self.wallet.wallet_uid];
                        [kAppWindow makeToast:XLocalized(@"删除成功!")];
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                }];
            }
            [self.eosService.accountResult.permissions enumerateObjectsUsingBlock:^(EOSPermissionsInfo * _Nonnull permissions, NSUInteger idx, BOOL * _Nonnull stopAll) {
                if ([permissions.perm_name isEqualToString:@"active"]) {
                    self.activeKeys = permissions.required_auth.keys;
                }else{
                    self.ownerKeys = permissions.required_auth.keys;
                }
            }];
            [self.tableView reloadData];
        }
        else{
            [self.view makeToast:errorMsg];
        }
    }];
}

- (void)setupUI{
    self.title = XLocalized(@"权限管理");
    UIView *headV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 65)];
    UIView *topV = [[UIView alloc]init];
    [headV addSubview:topV];
    [topV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(13));
        make.left.mas_equalTo(AdaptedWidth(20));
        make.right.mas_equalTo(AdaptedWidth(-20));
        make.height.mas_equalTo(AdaptedWidth(37));
    }];
    topV.backgroundColor = UIColorHex(#FCD839);
    topV.layer.cornerRadius = AdaptedWidth(10);
    UILabel *titleLab = [UILabel new];
    titleLab.textColor=UIColor.whiteColor;
    titleLab.font = kBoldFont(16);
    titleLab.text = XLocalized(@"账号");
    [topV addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(kNormalSpace);
    }];
    self.accountLab = [UILabel new];
    self.accountLab.textColor=UIColor.whiteColor;
    self.accountLab.font = kBoldFont(16);
    [topV addSubview:self.accountLab];
    [self.accountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-kNormalSpace);
    }];
    self.tableView.tableHeaderView=headV;
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 200;;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    //    UILabel *ownerTitle = [UILabel new];
    //    ownerTitle.textColor = UIColorHex(#333333);
    //    ownerTitle.font = kDefaultFont(15);
    ////    [self.contentView addSubview:ownerTitle];
    //    [ownerTitle mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(AdaptedWidth(33));
    //        make.top.equalTo(topV.mas_bottom).offset(AdaptedWidth(22));
    //    }];
    //    ownerTitle.text = XLocalized(@"拥有者权限");
    //    self.ownerLab = [UILabel new];
    //    self.ownerLab.textColor = UIColorHex(#909090);
    //    self.ownerLab.font = kDefaultFont(12);
    ////    [self.contentView addSubview:self.ownerLab];
    //    [self.ownerLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.equalTo(ownerTitle.mas_centerY);
    //        make.left.equalTo(ownerTitle.mas_right).offset(AdaptedWidth(10));
    //    }];
    //    UIView*ownerView = [UIView new];
    //    ownerView.backgroundColor = UIColorHex(#F7F7F7);
    //    ownerView.layer.cornerRadius = AdaptedWidth(10);
    ////    [self.contentView addSubview:ownerView];
    //    [ownerView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(ownerTitle.mas_bottom).offset(AdaptedWidth(12));
    //        make.left.mas_equalTo(AdaptedWidth(20));
    //        make.right.mas_equalTo(AdaptedWidth(-20));
    //    }];
    //    self.ownerKeyLab = [UILabel new];
    //    self.ownerKeyLab.textColor = UIColorHex(#B5B5B5);
    //    self.ownerKeyLab.font = kDefaultFont(16);
    //    self.ownerKeyLab.numberOfLines = 0;
    //    [ownerView addSubview:self.ownerKeyLab];
    //    [self.ownerKeyLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.mas_equalTo(AdaptedWidth(18));
    //        make.left.mas_equalTo(AdaptedWidth(15));
    //        make.bottom.mas_equalTo(AdaptedWidth(-28));
    //        make.right.mas_equalTo(AdaptedWidth(-15));
    //    }];
    //    self.ownerWeightLab = [UILabel new];
    //    self.ownerWeightLab.textColor = UIColorHex(#B5B5B5);
    //    self.ownerWeightLab.font = kDefaultFont(13);
    //    [ownerView addSubview:self.ownerWeightLab];
    //    [self.ownerWeightLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(AdaptedWidth(15));
    //        make.bottom.mas_equalTo(AdaptedWidth(-5));
    //    }];
    //
    //    UILabel *activeTitle = [UILabel new];
    //    activeTitle.textColor = UIColorHex(#333333);
    //    activeTitle.font = kDefaultFont(15);
    ////    [self.contentView addSubview:activeTitle];
    //    [activeTitle mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(AdaptedWidth(33));
    //        make.top.equalTo(ownerView.mas_bottom).offset(AdaptedWidth(22));
    //    }];
    //    activeTitle.text = XLocalized(@"管理者权限");
    //    self.activeLab = [UILabel new];
    //    self.activeLab.textColor = UIColorHex(#909090);
    //    self.activeLab.font = kDefaultFont(12);
    ////    [self.contentView addSubview:self.activeLab];
    //    [self.activeLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.centerY.equalTo(activeTitle.mas_centerY);
    //        make.left.equalTo(activeTitle.mas_right).offset(AdaptedWidth(10));
    //    }];
    //    UIView*activeView = [UIView new];
    //    activeView.backgroundColor = UIColorHex(#F7F7F7);
    //    activeView.layer.cornerRadius = AdaptedWidth(10);
    ////    [self.contentView addSubview:activeView];
    //    [activeView mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.equalTo(activeTitle.mas_bottom).offset(AdaptedWidth(16));
    //        make.left.mas_equalTo(AdaptedWidth(20));
    //        make.right.mas_equalTo(AdaptedWidth(-20));
    //    }];
    //    self.activeKeyLab = [UILabel new];
    //    self.activeKeyLab.textColor = UIColorHex(#B5B5B5);
    //    self.activeKeyLab.font = kDefaultFont(16);
    //    self.activeKeyLab.numberOfLines = 0;
    //    [activeView addSubview:self.activeKeyLab];
    //    [self.activeKeyLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.mas_equalTo(AdaptedWidth(18));
    //        make.left.mas_equalTo(AdaptedWidth(15));
    //        make.bottom.mas_equalTo(AdaptedWidth(-28));
    //        make.right.mas_equalTo(AdaptedWidth(-15));
    //    }];
    //    self.activeWeightLab = [UILabel new];
    //    self.activeWeightLab.textColor = UIColorHex(#B5B5B5);
    //    self.activeWeightLab.font = kDefaultFont(13);
    //    [activeView addSubview:self.activeWeightLab];
    //    [self.activeWeightLab mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.left.mas_equalTo(AdaptedWidth(15));
    //        make.bottom.mas_equalTo(AdaptedWidth(-5));
    //    }];
    UIView *footV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, AdaptedWidth(300))];
    UIView *bottomV=[UIView new];
    bottomV.layer.cornerRadius = AdaptedWidth(10);
    bottomV.backgroundColor = UIColorHex(#FCD839);
    [footV addSubview:bottomV];
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(23));
        make.left.mas_equalTo(AdaptedWidth(20));
        make.right.mas_equalTo(AdaptedWidth(-20));
    }];
    self.tableView.tableFooterView = footV;
    UIView*round1 = [UIView new];
    round1.backgroundColor = UIColorHex(#666666);
    [bottomV addSubview:round1];
    [round1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptedWidth(20));
        make.left.mas_equalTo(kNormalSpace);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(8), AdaptedWidth(8)));
    }];
    round1.layer.cornerRadius = AdaptedWidth(4);
    UILabel*lab1 = [UILabel new];
    lab1.textColor = UIColorHex(#666666);
    lab1.font = kDefaultFont(13);
    lab1.numberOfLines = 0;
    [bottomV addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(round1.mas_right).offset(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.top.equalTo(round1.mas_top).offset(AdaptedWidth(-5));;
    }];
    lab1.text = XLocalized(@"Owner(拥有者权限)：拥有当前EOS账号的所有权限。");
    
    UIView*round2 = [UIView new];
    round2.backgroundColor = UIColorHex(#666666);
    [bottomV addSubview:round2];
    [round2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab1.mas_bottom).offset(AdaptedWidth(20));
        make.left.mas_equalTo(kNormalSpace);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(8), AdaptedWidth(8)));
    }];
    round2.layer.cornerRadius = AdaptedWidth(4);
    UILabel*lab2 = [UILabel new];
    lab2.textColor = UIColorHex(#666666);
    lab2.font = kDefaultFont(13);
    lab2.numberOfLines = 0;
    [bottomV addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(round2.mas_right).offset(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.top.equalTo(round2.mas_top).offset(AdaptedWidth(-5));;
    }];
    lab2.text = XLocalized(@"Active(管理者权限)：默认情况下，可以完成除更改Owner以外的所有交易。 -为了您的资产安全，建议使用Active权限导入钱包日常");
    
    UIView*round3 = [UIView new];
    round3.backgroundColor = UIColorHex(#666666);
    [bottomV addSubview:round3];
    [round3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab2.mas_bottom).offset(AdaptedWidth(20));
        make.left.mas_equalTo(kNormalSpace);
        make.size.mas_equalTo(CGSizeMake(AdaptedWidth(8), AdaptedWidth(8)));
    }];
    round3.layer.cornerRadius = AdaptedWidth(4);
    UILabel*lab3 = [UILabel new];
    lab3.textColor = UIColorHex(#666666);
    lab3.font = kDefaultFont(13);
    lab3.numberOfLines = 0;
    [bottomV addSubview:lab3];
    [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(round3.mas_right).offset(kNormalSpace);
        make.right.mas_equalTo(-kNormalSpace);
        make.bottom.mas_equalTo(-AdaptedWidth(20));
        make.top.equalTo(round3.mas_top).offset(AdaptedWidth(-5));
    }];
    lab3.text = XLocalized(@"为了您的资产安全，建议使用Active权限导入钱包日常使用。并妥善保管Owner权限对应的私钥。");
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return self.ownerKeys.count;
    }
    return self.activeKeys.count;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *activeTitle = [UILabel new];
    activeTitle.backgroundColor = UIColor.whiteColor;
    activeTitle.textColor = UIColorHex(#333333);
    activeTitle.font = kDefaultFont(15);
    if (section==0) {
        activeTitle.text = [NSString stringWithFormat:@"     %@",XLocalized(@"拥有者权限")];
    }else{
        activeTitle.text = [NSString stringWithFormat:@"     %@",XLocalized(@"管理者权限")];
    }
    return activeTitle ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIView*backView = [UIView new];
        backView.backgroundColor = UIColorHex(#F7F7F7);
        backView.layer.cornerRadius = AdaptedWidth(10);
        [cell.contentView addSubview:backView];
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(AdaptedWidth(0));
            make.left.mas_equalTo(AdaptedWidth(20));
            make.right.mas_equalTo(AdaptedWidth(-20));
            make.bottom.mas_equalTo(AdaptedWidth(-8));
        }];
        UILabel *keyLab = [UILabel new];
        keyLab.tag = 100;
        keyLab.textColor = UIColorHex(#B5B5B5);
        keyLab.font = kDefaultFont(16);
        keyLab.numberOfLines = 0;
        [backView addSubview:keyLab];
        [keyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(AdaptedWidth(18));
            make.left.mas_equalTo(AdaptedWidth(15));
            make.bottom.mas_equalTo(AdaptedWidth(-18));
            make.right.mas_equalTo(AdaptedWidth(-15));
        }];
        keyLab.userInteractionEnabled = YES;
        kWeakSelf(self);
        [keyLab addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            UIPasteboard *pab = [UIPasteboard generalPasteboard];
            [pab setString:keyLab.text];
            if (pab == nil) {
                [weakself.view makeToast:XLocalized(@"复制失败!")];
            }else
            {
                [weakself.view makeToast:XLocalized(@"复制成功!")];
            }
        }]];
    }
    UILabel*keyL = [cell.contentView viewWithTag:100];
    if (indexPath.section==0) {
        keyL.text = self.ownerKeys[indexPath.row].key;
    }else{
        keyL.text = self.activeKeys[indexPath.row].key;
    }
    return cell;
}
#pragma mark - getter
- (EOSService *)eosService{
    if (!_eosService) {
        _eosService = [EOSService new];
        _eosService.wallet = self.wallet;
    }
    return _eosService;
}
@end
